const express = require("express");
const exApp = require("../index");
const router = express.Router();
const mysql = require("../connect/pomsql");
const { SuccessModule, ErrorModule } = require("../model/resModel");
const { resHandler } = require("../model/resHandler.js");
const path = require("path");
const multer = require("multer");

// 李雪峰区域
//dest：图片要保存的路径


// 查找新闻

router.get("/lxfall", async function(req, res) {
	let {
		size,
		page
	} = req.query

	let sql = `SELECT * FROM app_new LIMIT ${(page-1)*size},${size}`;
	let data = await  mysql(sql);
	res.json({
		code: 200,
		data,
	});
});





// exApp.get("/lxfalls", async function (req, res) {
// 	let sql = "SELECT * FROM lxf_one WHERE title LIKE '李雪峰' ";
// 	let data = await pomsql(sql);
// 	res.json({
// 		code: 1,
// 		data
// 	});
// });



// select * from tablename order by filename1 desc

// 话题
exApp.get("/lxftuijianht",async function(req,res){
	let {
		size,
		page
	} = req.query
	let sql=` SELECT * FROM theme  ORDER BY  RAND() LIMIT  ${(page-1)*size},${size}`;
	let data=await mysql(sql)
	res.json({
		code:1,
		data
	})

})
exApp.get("/lxfnewsht",async function(req,res){
	let {
		size,
		page
	} = req.query
	let sql=` select * from theme order by id desc LIMIT ${(page-1)*size},${size}`;
	let data=await mysql(sql)
	res.json({
		code:1,
		data
	})

})

exApp.get("/lxfphoneht",async function(req,res){
	let {
		size,
		page
	} = req.query
	let sql=` select * from theme order by id desc LIMIT ${(page-1)*size},${size}`;
	let data=await mysql(sql)
	res.json({
		code:1,
		data
	})

})



// 推荐

exApp.get("/lxftuijian",async function(req,res){
	let {
		size,
		page
	} = req.query
	let sql=` SELECT * FROM products  ORDER BY  RAND() LIMIT  ${(page-1)*size},${size}`;
	let data=await mysql(sql)
	res.json({
		code:1,
		data
	})

})

// 最新
exApp.get("/lxfnews",async function(req,res){
	let {
		size,
		page
	} = req.query
	let sql=` select * from products order by id desc LIMIT ${(page-1)*size},${size}`;
	let data=await mysql(sql)
	res.json({
		code:1,
		data
	})

})

// 获取新闻
exApp.get("/lxfnewsbig",async function(req,res){
	let {
		size,
		page
	} = req.query
	let sql=` select * from lxf_news order by id desc LIMIT ${(page-1)*size},${size}`;
	let data=await mysql(sql)
	res.json({
		code:1,
		data
	})

})


exApp.post("/add", async (req, res) => {
	const {
	type,title
	} = req.body;
	let sql =
		`INSERT INTO products(type,title ) VALUES('${type}','${title}')`;
	let data = await mysql(sql);
	res.json({
		code: 200,
		data,
	});
});


// 数码产品         
exApp.get("/lxfphone", async function (req, res) {
	let {
		size,
		page
	} = req.query
	let sql = `SELECT * FROM products WHERE type LIKE 'phone' order by id desc LIMIT ${(page-1)*size},${size}`;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
});



// 图书文具
exApp.get("/lxfbook", async function (req, res) {
	let {
		size,
		page
	} = req.query
	let sql = `SELECT * FROM products WHERE type LIKE 'book' order by id desc LIMIT ${(page-1)*size},${size}`;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
});

//服饰配包
exApp.get("/lxffushixiebao", async function (req, res) {
	let {
		size,
		page
	} = req.query
	let sql = `SELECT * FROM products WHERE type LIKE 'fushixiebao' order by id desc LIMIT ${(page-1)*size},${size}`;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
});

//美妆闲置
exApp.get("/lxfbeauty", async function (req, res) {
	let {
		size,
		page
	} = req.query
	let sql = `SELECT * FROM products WHERE type LIKE 'beauty' order by id desc LIMIT ${(page-1)*size},${size}`;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
});
//体育用品
exApp.get("/lxfequipment", async function (req, res) {
	let {
		size,
		page
	} = req.query
	let sql = `SELECT * FROM products WHERE type LIKE 'equipment' order by id desc LIMIT ${(page-1)*size},${size}`;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
});


// SELECT * FROM user WHERE name LIKE ';%三%';
// 模糊搜索类
// exApp.get('/lxfmh',async function(req,res){
// 	let {
// 		title
// 	}=req.query
// 	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by id desc `;
// 	let data = await mysql(sql);
// 	res.json({
// 		code: 1,
// 		data
// 	});
// })

// 通过时间
exApp.get('/lxfmhtime',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})
// 降序
exApp.get('/lxfmhtimes',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by create_time desc `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})
// 升序
exApp.get('/lxfmhtimex',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by create_time asc  `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})

// 价格产品
exApp.get('/lxfmhmoney',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%' `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})
exApp.get('/lxfmhmoneys',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by price desc `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})
// 升序
exApp.get('/lxfmhmoneyx',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by price asc  `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})


// 收藏量 collection
exApp.get('/lxfmhsc',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%' `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})
exApp.get('/lxfmhscs',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by collection desc `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})
// 升序
exApp.get('/lxfmhscx',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by collection asc  `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})


// 查看量 look

exApp.get('/lxflook',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%' `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})
exApp.get('/lxflooks',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by look desc `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})
// 升序
exApp.get('/lxflookx',async function(req,res){
	let {
		title
	}=req.query
	let sql = `SELECT * FROM products WHERE title LIKE  '%${title}%'  order by look asc  `;
	let data = await mysql(sql);
	res.json({
		code: 1,
		data
	});
})


// 最新

// select * from yourtable where 查询条件 order by id desc limit 0,10;

exApp.get("/newlist", async function (req, res) {
  let time = new Date();
  let sql = ` SELECT * FROM  products  order by id desc LIMIT 0,6 `;
  // let sql=` select * from products order by id desc LIMIT ${(page-1)*size},${size}`;
  let data = await mysql(sql);
  res.json({
    code: 200,
    data,
  });
});

// 推荐
// SELECT * FROM T_USER  ORDER BY  RAND() LIMIT 10
exApp.get("/recommend", async function (req, res) {
  let sql = ` SELECT * FROM  products  ORDER BY  RAND()  LIMIT 10`;
  let data = await mysql(sql);
  res.json({
    code: 200,
    data,
  });
});
// 推荐
// SELECT * FROM T_USER  ORDER BY  RAND() LIMIT 10
exApp.get("/theme", async function (req, res) {
  let sql = ` SELECT * FROM  theme  ORDER BY  RAND()  LIMIT 4`;
  let data = await mysql(sql);
  res.json({
    code: 200,
    data,
  });
});
// 所有
exApp.get("/productall", async function (req, res) {
  let sql = `SELECT * FROM products`;
  let data = await mysql(sql);
  res.json({
    code: 200,
    data,
  });
});

// 搜索
exApp.post("/search", async function (req, res) {
  let { word } = req.body;
  console.log(req.body);
  let sql = `SELECT * FROM products  WHERE title LIKE '%${word}%'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
// exApp.post("/add", async (req, res) => {
//   const { type, title } = req.body;
//   let sql = `INSERT INTO products(type,title ) VALUES('${type}','${title}')`;
//   let data = await mysql(sql);
//   res.json({
//     code: 200,
//     data,
//   });
// });

// 筛选
exApp.post("/product", async function (req, res) {
  let { type } = req.body;
  console.log(req.body);
  let sql = `select * from products where type = '${type}'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});

// 获取用户数据
exApp.post("/getuser", async function (req, res) {
  let { email } = req.body;
  console.log(req.body);
  let sql = `select * from users where email = '${email}'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});

// 以上是固定写法
// 每添加一个需要在index.js中引入

// router.get('/a', async (req, res, next) => {
//   let sql = 'select * from users'
//   let result = await mysql(sql)
//   if (result) {
//     res.json(new SuccessModule(result))
//     return
//   }

//   res.json(new ErrorModule('操作失败'))
// })
// //zyb 订单状态管理
// router.get('/status', async (req, res, next) => {
//   let sql = 'select * from order_status'
//   let result = await mysql(sql)

//   resHandler(res, result, '操作失败')

// })
// //zyb 订单概述
// router.get('/text', async (req, res, next) => {
//   let sql = 'select * from order_text'
//   let result = await mysql(sql)
//   resHandler(res, result, '操作失败')
// })

// //zyb 财务管理积分余额明细
// router.get('/money', async (req, res, next) => {
//   let sql = 'select * from money'
//   let result = await mysql(sql)
//   resHandler(res, result, '操作失败')
// })
// //zyb 优惠券明细
// router.get('/coupons', async (req, res, next) => {
//   let sql = 'select * from coupons'
//   let result = await mysql(sql)
//   resHandler(res, result, '操作失败')
// })
// // zyb优惠券明细管理添加
// router.post('/addtable', async function (req, res) {
//   const { id, start_time, end_time, fa_count, miaoshu, tiaojian, sheng_count } = req.body
//   let sql = `insert into coupons (id,start_time,end_time,fa_count,miaoshu,tiaojian,sheng_count) values('${id}','${start_time}', '${end_time}', '${fa_count}', '${miaoshu}', '${tiaojian}',  '${sheng_count}')`
//   let data = await mysql(sql)
//   res.json({
//     code: 200,
//     data
//   })
// })
// //zyb 优惠券分类
// router.get('/youhui_fen', async (req, res, next) => {
//   let sql = 'select * from youhui_fen'
//   let result = await mysql(sql)
//   resHandler(res, result, '操作失败')
// })
// // zyb优惠券分类管理添加
// router.post('/add_fen', async function (req, res) {
//   const { id, name } = req.body
//   let sql = `insert into youhui_fen (id,name) values('${id}', '${name}')`
//   let data = await mysql(sql)
//   res.json({
//     code: 200,
//     msg: '更新成功',
//     data
//   })
// })
// //zyb 充值记录
// router.get('/deposit_history', async (req, res, next) => {
//   let sql = 'select * from deposit_history'
//   let result = await mysql(sql)
//   resHandler(res, result, '操作失败')
// })
// //zyb 优惠卷管理
// router.get('/pre', async (req, res, next) => {
//   let sql = 'select * from preferential'
//   let result = await mysql(sql)
//   resHandler(res, result, '操作失败')
// })
// // zyb优惠券分类管理删除
// router.post('/del_fen', async (req, res) => {
//     let { id } = req.body;
//     let sql = `delete from youhui_fen where id = ${id}`
//     let data = await mysql(sql)
//     resHandler(res, data, '获取失败')
//   })
//   // zyb优惠券分类修改
//   router.post('/bianji', async function (req, res) {
//     let id =req.body.id
//
//     let name = req.body.name
//     let sql = `update youhui_fen set name='${name}' where id = '${id}'`
//     let data = await mysql(sql)
//     res.json({
//       code: 200,
//       msg:'更新成功',
//       data
//     })
//   })

// 一下是固定写法
// exApp.use("/", router);

// 添加图片

const MAO = require("multer-aliyun-oss");
const { type } = require("os");
const { title } = require("process");
//dest：图片要保存的路径
let files = [];
const storage = multer.diskStorage({
  //存储的位置
  destination(req, file, cb) {
    cb(null, "./uploads/");
  },
  filename(req, file, cb) {
    cb(null, file.originalname);
  },
});

const upload = multer({
  storage,
});
exApp.post("/addimg", upload.single("file"), (req, res) => {
  console.log(req);
  //给客户端返回图片的访问地址 域名 + 文件名字
  //因为在 app.js文件里面我们已经向外暴漏了存储图片的文件夹 uploa
  const url = "http://localhost:4000/uploads/" + req.file.filename;
  //      res.json({url})
  console.log(url);
});

// 开放静态图片端口
exApp.use(express.static(path.resolve("./public")));